package com.ttmall.ware.service.impl;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ttmall.common.constant.WareConstant;
import com.ttmall.ware.entity.PurchaseDetailEntity;
import com.ttmall.ware.service.PurchaseDetailService;
import com.ttmall.ware.service.WareSkuService;
import com.ttmall.ware.vo.MergeVO;
import com.ttmall.ware.vo.PurchaseDoneVo;
import com.ttmall.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttmall.common.utils.PageUtils;
import com.ttmall.common.utils.Query;

import com.ttmall.ware.dao.PurchaseDao;
import com.ttmall.ware.entity.PurchaseEntity;
import com.ttmall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status" , 0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVO vo) {
        Long purchaseId = vo.getPurchaseId();
        if (purchaseId == null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime( new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        else {
            //PurchaseEntity purchaseEntity = this.getById(purchaseId);
            //
            //purchaseEntity.getStatus() !=

        }
        List<Long> items = vo.getItems();

        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(i);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);

        PurchaseEntity pe = new PurchaseEntity();
        pe.setId(purchaseId);
        pe.setUpdateTime( new Date());
        this.updateById(pe);
    }

    @Transactional
    @Override
    public void received(List<Long> ids) {
        //当前采购单是新建或已分配
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            return item.getStatus() == WareConstant.PurchaseStatusEnum.CREATE.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode();
        }).map(item->{
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
                })
                .collect(Collectors.toList());
        //改变采购单状态
        this.updateBatchById(collect);

        // 改变采购项状态
        collect.forEach(item->{
            List<PurchaseDetailEntity> details = purchaseDetailService.listDetailByPurchaseId(item.getId());

            List<PurchaseDetailEntity> collect1 = details.stream().map(e -> {
                PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
                detailEntity.setId(e.getId());
                detailEntity.setStatus( WareConstant.PurchaseDetailStatusEnum.BUYING.getCode() );
                return detailEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(collect1);

        });
    }

    @Transactional
    @Override
    public void done(PurchaseDoneVo vo) {
        // 1.改变采购单状态
        Long id = vo.getId();

        // 2. 改变采购项状态
        List<PurchaseItemDoneVo> items = vo.getItems();
        Boolean flag = true;

        List<PurchaseDetailEntity> updates = new ArrayList<>();
        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.ERROR.getCode()){
                flag = false;
                detailEntity.setStatus(item.getStatus() );
            }
            else {
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                // 3. 采购入库
                PurchaseDetailEntity detail = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(
                        detail.getSkuId() , detail.getWareId(),detail.getSkuNum()
                );
            }

            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }
        purchaseDetailService.updateBatchById(updates);

        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() :
                WareConstant.PurchaseStatusEnum.ERROR.getCode());
        this.updateById(purchaseEntity);


    }

}